package personnel.department.pages.addpage;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Set;

import org.eclipse.jface.viewers.ColumnLabelProvider;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.widgets.TabFolder;

import personnel.department.dao.entity.Validation;
import personnel.department.pages.ModelProviderFactory;
import personnel.department.pages.PDTable;
import personnel.department.pages.widgets.AbstractWidget;
import personnel.department.utils.DateUtils;

public class ValidationEditor extends AbstractTableEditor<Validation> {

	public ValidationEditor(CTabFolder tabFolder) {
		super(tabFolder, "Аттестация");
	}

	public ValidationEditor(CTabFolder tabFolder, Set<Validation> validations) {
		super(tabFolder, "Аттестация", validations);
	}



	@Override
	public PDTable<Validation> createTable(Composite parent) {
		return new PDTable<Validation>(parent) {

			@Override
			public List<Validation> setModelProvider() {
				return getModel();
			}

			@Override
			protected void createColumns(Composite parent, TableViewer viewer) {
				String[] titles = { "Дата присвоения", "Дата подтверждения", "1 категория", "2 категория", "Высшая категория"};
				int[] bounds = { 100, 100, 100, 100, 100};

				TableViewerColumn col = createTableViewerColumn(getViewer(), titles[0], bounds[0], 0);
				col.setLabelProvider(new ColumnLabelProvider() {
					@Override
					public String getText(Object element) {
						Validation e = (Validation) element;
						return format.format(e.getAssignmentDate());
					}
				});

				col = createTableViewerColumn(getViewer(), titles[1], bounds[1], 1);
				col.setLabelProvider(new ColumnLabelProvider() {
					@Override
					public String getText(Object element) {
						Validation e = (Validation) element;
						return format.format(e.getConfirmation());
					}
				});

				col = createTableViewerColumn(getViewer(), titles[2], bounds[2], 2);
				col.setLabelProvider(new ColumnLabelProvider() {
					@Override
					public String getText(Object element) {
						Validation e = (Validation) element;
						return String.valueOf(e.getFirstCategory());
					}
				});

				col = createTableViewerColumn(getViewer(), titles[3], bounds[3], 3);
				col.setLabelProvider(new ColumnLabelProvider() {
					@Override
					public String getText(Object element) {
						Validation e = (Validation) element;
						return String.valueOf(e.getSecondCategory());
					}
				});

				col = createTableViewerColumn(getViewer(), titles[4], bounds[4], 4);
				col.setLabelProvider(new ColumnLabelProvider() {
					@Override
					public String getText(Object element) {
						Validation e = (Validation) element;
						return String.valueOf(e.getHigherCategory());
					}
				});

			}
		};
	}

	@SuppressWarnings("unchecked")
	@Override
	protected void validate() {
		AbstractWidget<?> firstCategory = getWidgetByFieldName("firstCategory");
		AbstractWidget<?> secondCategory = getWidgetByFieldName("secondCategory");
		AbstractWidget<Date> assignmentDate = (AbstractWidget<Date>) getWidgetByFieldName("assignmentDate");
		getModel().stream().forEach(validation -> {
			if (validation.getHigherCategory()) {
				firstCategory.setEnabled(false);
				secondCategory.setEnabled(false);
				assignmentDate.setEnabled(false);
			} else if (validation.getSecondCategory()) {
				firstCategory.setEnabled(false);
			}
		});


		AbstractWidget<Date> confirmation = (AbstractWidget<Date>) getWidgetByFieldName("confirmation");

		if (!getModel().isEmpty()) {
			Date maxConfirmation = getModel().stream()
					.max((arg0, arg1) -> arg0.getConfirmation().compareTo(arg1.getConfirmation()))
					.map(val -> val.getConfirmation())
					.get();

			assignmentDate.setValue(maxConfirmation);
			confirmation.setValue(DateUtils.add(maxConfirmation, Calendar.YEAR, 5));
		}
	}

}
